package com.zzyl.nursing.service.impl;

import java.util.HashMap;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.StringUtils;
import com.zzyl.framework.web.service.TokenService;
import com.zzyl.nursing.dto.UserLoginRequestDto;
import com.zzyl.nursing.service.WechatService;
import com.zzyl.nursing.vo.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.FamilyMemberMapper;
import com.zzyl.nursing.domain.FamilyMember;
import com.zzyl.nursing.service.IFamilyMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;
import java.util.Map;

/**
 * 老人家属Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-03
 */
@Service
public class FamilyMemberServiceImpl extends ServiceImpl<FamilyMemberMapper, FamilyMember> implements IFamilyMemberService
{
    @Autowired
    private FamilyMemberMapper familyMemberMapper;

    @Autowired
    private WechatService wechatService;

    @Autowired
    private TokenService tokenService;

    /**
     * 微信小程序登录
     * @param userLoginRequestDto
     * @return
     */
    @Override
    public LoginVo login(UserLoginRequestDto userLoginRequestDto) {
        LoginVo vo = new LoginVo();
        //1.通过code获得openId
        String openId = wechatService.getOpenid(userLoginRequestDto.getCode());
        //2.获取用户的手机号码
        String phone = wechatService.getPhone(userLoginRequestDto.getPhoneCode());
        //3.通过openId判断数据库是否存在家属信息
        FamilyMember member = lambdaQuery().eq(FamilyMember::getOpenId, openId).one();
        //4.存在就更新
        if (null != member){
            //如果手机号码不同，则更新手机号码
            if(ObjectUtil.notEqual(member.getPhone(), phone)){
                updatePhone(member, phone);
            }
        }else {
            //5.不存在就注册一个，生成昵称
            member = FamilyMember.builder().openId(openId).phone(phone).name(getNickName(phone)).build();
            save(member);
        }
        //6.生成token，返回
        Map<String, Object> claims = new HashMap<>();
        claims.put("userId", member.getId());
        claims.put("nickName", member.getName());
        String token = tokenService.createToken(claims);
        vo.setToken(token);
        vo.setNickName(member.getName());
        return vo;
    }

    private static List<String> DEFAULT_NICKNAME_PREFIX = List.of(
            "生活更美好",
            "大桔大利",
            "日富一日",
            "好柿开花",
            "柿柿如意",
            "一椰暴富",
            "大柚所为",
            "杨梅吐气",
            "天生荔枝"
    );

    private String getNickName(String phone) {
        //Math.random() 产生的值[0-1) * 9 = [0-9) => int 0-8
        int idx = (int) (Math.random() * DEFAULT_NICKNAME_PREFIX.size());
        // 随机组装昵称，词组+手机号后四位
        String nickName = DEFAULT_NICKNAME_PREFIX.get(idx)
                + StringUtils.substring(phone, 7);
        return nickName;
    }

    private void updatePhone(FamilyMember member, String phone) {
        FamilyMember update = new FamilyMember();
        update.setId(member.getId());
        update.setPhone(phone);
        updateById(update);
    }
}
